//
//  YCWelComeViewController.swift
//  swift微博
//
//  Created by 姚伟聪 on 15/12/17.
//  Copyright © 2015年 姚伟聪. All rights reserved.
//

import UIKit
import SDWebImage

class YCWelComeViewController: UIViewController {

    
    //不要初始值，可以用?
    var contranit: NSLayoutConstraint?
    override func viewDidLoad() {
        super.viewDidLoad()
        addUI()
        setIconView()
    //这里因为是从沙盒里面取对象，所以是同一个对象
        YCUserAccount.loadUserAccount()?.loadUsetInfo({ (error) -> Void in
            self.setIconView()
            self.setNameLable()
        })
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        addIconViewAnimation()
        
        
    }
    //头像动画弹动
    private func addIconViewAnimation(){

        let offSet: CGFloat = view.bounds.height - 160
        
        contranit?.constant = -offSet
        
        //弹簧效果的动画
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0)
            , animations: { () -> Void in
                
            //刷新layer层
               self.view.layer.layoutIfNeeded()
                
            }) { (_) -> Void in
                
                self.nameLab.alpha = 0
                self.nameLab.hidden = false
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.nameLab.alpha = 1
                    }, completion: { (_) -> Void in
                        ( UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
                })
        }
        
    
    }
    
    //加载用户头像
    private func setIconView(){
    
        if let avatar_large = YCUserAccount.loadUserAccount()?.avatar_large{
            let url = NSURL(string: avatar_large)!
            print("url:\(url)")
            iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            
        
        }
        
    }
    //加载用户名称
    private func setNameLable(){
        
        let screen_name = YCUserAccount.loadUserAccount()?.screen_name
        
        nameLab.text = screen_name!
    
    }
    
    
    //添加界面
    private func addUI(){
    
        view.addSubview(backGroundView)
        view.addSubview(iconView)
        view.addSubview(nameLab)
        
        
        backGroundView.translatesAutoresizingMaskIntoConstraints = false
         iconView.translatesAutoresizingMaskIntoConstraints = false
         nameLab.translatesAutoresizingMaskIntoConstraints = false
        
        //背景约束
        view.addConstraint(NSLayoutConstraint(item: backGroundView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backGroundView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backGroundView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backGroundView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //头像约束
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
          view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
       contranit = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(contranit!)
        //名字约束
        view.addConstraint(NSLayoutConstraint(item: nameLab, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: nameLab, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        
    
    }
    
    //MARK:-懒加载
    
    private lazy var backGroundView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = {
//        let icon =  UIImageView(image: UIImage(named: "avatar_default_big"))
        let icon = UIImageView()
        //裁剪头像
        icon.layer.cornerRadius = 42.5
        icon.layer.masksToBounds = true
        return icon
    
    }()
    
    private lazy var nameLab: UILabel = {
        let name = UILabel()
        name.text = ""
        name.hidden = true
        return name
    
    }()
    

   
}
